/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.yami.shop.bean.app.dto.ProductDto;
import com.yami.shop.bean.model.BalanceLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.BalanceLogService;
import com.yami.shop.service.ProductService;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yami.shop.bean.app.dto.CategoryDto;
import com.yami.shop.bean.model.Category;
import com.yami.shop.service.CategoryService;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import cn.hutool.core.bean.BeanUtil;

/**
 * @author lanhai
 */
@RestController
@RequestMapping("/category")
@Tag(name = "分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService prodService;



    @Autowired
    private BalanceLogService balanceLogService;

    @Autowired
    private MyToken myToken;
    /**
     * 分类信息列表接口
     */
    @GetMapping("/categoryInfo")
    @Operation(summary = "分类信息列表" , description = "获取所有的产品分类信息，顶级分类的parentId为0,默认为顶级分类")
    @Parameter(name = "parentId", description = "分类ID", required = false)
    public ServerResponseEntity<Map<String, Object>> categoryInfo(HttpServletRequest req, @RequestParam(value = "parentId", defaultValue = "0") Long parentId) {
        List<Category> categories = categoryService.listByParentId(parentId);
        List<CategoryDto> categoryDtos = BeanUtil.copyToList(categories, CategoryDto.class);
        IPage<ProductDto> productPage=null;
        productPage=new IPage<ProductDto>() {
            @Override
            public List<OrderItem> orders() {
                return new ArrayList<OrderItem>();
            }

            @Override
            public List<ProductDto> getRecords() {
                return new ArrayList<ProductDto>();
            }

            @Override
            public IPage<ProductDto> setRecords(List<ProductDto> records) {

                return null;
            }

            @Override
            public long getTotal() {
                return 0;
            }

            @Override
            public IPage<ProductDto> setTotal(long total) {
                return null;
            }

            @Override
            public long getSize() {
                return 0;
            }

            @Override
            public IPage<ProductDto> setSize(long size) {
                return null;
            }

            @Override
            public long getCurrent() {
                return 0;
            }

            @Override
            public IPage<ProductDto> setCurrent(long current) {
                return null;
            }
        };
        if(parentId!=null && parentId!=0) {
            PageParam<ProductDto> page = new PageParam<ProductDto>();
            page.setCurrent(1);
            page.setSize(10);
            Integer mallid=myToken.getMallId(req);
            productPage = prodService.pageByCategoryId(page, parentId,mallid);
        }
        Map<String, Object> p = new HashMap<>();
        p.put("children",categoryDtos);
        p.put("productions", productPage);

        return ServerResponseEntity.success(p);
    }



}
